<template>
  <window class="import-win" ref="win" :title="title">
    <el-select size="small" class="am-u-sm-12 am-form-group"
               v-model="areaTeamId" placeholder="请选择区域" >
      <el-option
        v-for="item in areaTeams"
        :key="item.areaTeamId"
        :label="item.name"
        :value="item.areaTeamId">
      </el-option>
    </el-select>
    
    <input
      v-if="type === 'upload'"
      class="am-u-sm-12 am-form-group"
      id="uploadFile"
      :disabled="!areaTeamId"
      @change="uploadExcel"
      type="file"
      accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
    <div v-if="type === 'downLoad'" class="am-text-center am-form-group">
      <el-button @click="handleConfirm" type="success">确认</el-button>
    </div>
  </window>
</template>
<script>
  import io from '../../lib/io'
  export default {
    data () {
      return {
        areaTeamId: '',
        type: '',
        title: '',
        DepartmentList:[],
      }
    },
    computed: {
      areaTeams: function () {
        return this.$root.config.areaTeams || []
      },
    },
    methods: {
      show () {
        this.$refs.win.show()
        
      },
      close () {
        this.$refs.win.close()
      },
      handleConfirm () {
        if (!this.areaTeamId) {
          this.$alert('请选择区域！')
          return
        }
        io.downloadFile(io.downloadImportTemplate, {areaTeamId: this.areaTeamId})
        this.$refs.win.close()
      },
      uploadExcel:function() {
        let formData = new FormData();
        formData.append("file",document.getElementById('uploadFile').files[0]);
        this.$showLoading()
        let params = '?accessToken=' + io.getHeaders().accessToken + '&areaTeamId=' + this.areaTeamId
        io.postFile(io.importCustomerResource + params, formData, (ret) => {
          $('#uploadFile').val(null)
          this.$hiddenLoading()
          if (ret.ok && ret.data.success){
            this.$emit('completed')
            this.close()
            this.$alert('导入成功');
          } else {
            this.$alert(ret.data.desc || "上传失败");
          }
        })
      },
    },
    beforeDestroy () {
      this.$refs.win.$el.innerHTML = ''
    },
  }
</script>
